<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\ApiController;
use App\Models\Agent;
use App\Models\Cate;
use App\Models\FeeGood;
use App\Models\Good;
use App\Models\Set;
use Illuminate\Http\Request;

class Index extends ApiController{
    public function index(Request $req){
        $cate = Cate::select('id','title as name')->get();
        $goods = [];
        foreach ($cate as $v) {
            $gs = Good::where('cid',$v->id)->select('id','title','logo')->get();
            foreach ($gs as &$g) {
                $g->logo = getImage($g->logo,1);
            }
            $goods[$v->id] = $gs;
        }
        $set = Set::getSet('base');
        $types = [0,1,2];
        $canfee = [100,100,100];
        foreach ($types as $v) {
            $gs = FeeGood::where('type',$v)->where('feegood.status',0)->leftJoin('feeitem','feegood.id','=','feeitem.gid')->where('feeitem.status',0)->select('feegood.title','costprice','marketprice')->get()->toArray();
            if ($gs) {
                foreach ($gs as $g) {
                    $canfee[$v] = min(round(($g['costprice']+$g['marketprice']) / floatval($g['title']) * 100),$canfee[$v]);
                }
            }else $canfee[$v] = 0;
        }
        $sharedata = array(
                'title'=> $set['title'],
                'imgUrl'=>"https://".$req->host()."/static/hf_icon.jpeg",
                'desc'=>$set['desc']??$set['title'],
                'link'=>"https://".$req->host()."/".$req->shopagent->code
        );
        wxresp(1,['cate'=>$cate,'goods'=>$goods,'title'=>$set['title']??'','desc'=>$set['desc']??'','canfee'=>$canfee,'copyright'=>$set['copyright']??'','sharedata'=>$sharedata,'isagent'=>Agent::where('mid',$req->mem->id)->first() ? 1 : 0]);
    }
}